package com.luying.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luying.dao.RoleDao;
import com.luying.pojo.*;
import com.luying.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Override
    public int add(UserRole userRole) {
        UserRole userRole1 = roleDao.selectByUserId(userRole.getUserId());
        if(userRole1==null){
            roleDao.addUserRole(userRole);
        }else {
            roleDao.updateUserRole(userRole);
        }
        return 0;
    }

    @Override
    public Map<String, Object> userList() {
        Map<String,Object> resp = new HashMap<>(16);
        List<BaseDown> users =  roleDao.selectAllUser();
        List<BaseDown> roles =  roleDao.selectAllRole();
        resp.put("users",users);
        resp.put("roles",roles);
        return resp;
    }

    @Override
    public PageResult query(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageIndex(),pageQuery.getPageSize());
        List<UserRole> list =  roleDao.selectPage(pageQuery);
        PageInfo<UserRole> pageInfo = new PageInfo(list);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),list);
        return pageResult;
    }
}
